package universityModel.organization;

import universityModel.curriculum.Curriculum;
import universityModel.lesson.Lesson;
import universityModel.lesson.LessonInterface;
import universityModel.person.Name;
import universityModel.person.StudentInterface;
import universityModel.utils.*;

/**
 *
 * @author Parkhomenko Dmytro
 */
public class Group extends OutputControl implements ExtendedGroupView {
    private String name;
    private int level;
    private ArrayModel<StudentInterface> students = new ArrayModel<StudentInterface>();
    private Curriculum curriculum;
        
    /**
    * Constructor for class Group
    * @param name name for a new group
     *@param level group degree  
    */
    public Group(String name, int level) {
        this.name = name;
        this.level = level;
        curriculum = new Curriculum(name);		
    }
	
    @Override
    public String getName() {
	return name;
    }

    @Override
    public void addStudent(StudentInterface student) {
        students.add(student);
    }
	
    @Override
    public void delStudent(StudentInterface student) {
	students.remove(getStudentIndex(student.getName()));
    }
	
    @Override
    public void setCurriculum(Curriculum curriculum) {
	this.curriculum = curriculum;
    }
		
    @Override
    public void addLesson(LessonInterface lesson) {
        curriculum.addLesson(lesson);
    }
    
    @Override
    public Curriculum getCurriculum() {
        return curriculum.clone();
    }
    
    @Override
    public ArrayModel<StudentInterface> getStudents() {
        return new ArrayModel<StudentInterface>(students);//clone inside
    }
    
    @Override
    public void sort() {
	students.sort();
    }

    @Override
    public int getLevel() {
        return level;
    }
	
    @Override
    public boolean equals(Object obj) {
       if (this == obj) {
            return true;
	}
		
        if (obj == null) {
		return false;
	}
        
	if (getClass() != obj.getClass()) {
		return false;
	}
        
        Group comGroup = (Group) obj;
        if(name.equals(comGroup.getName())) {
            return true;
        }
        return false;
    }
    
    @Override
    public StudentInterface getStudent(int index) {
        return students.get(index);
    }
    
    @Override
    public boolean isEmpty() {
        return students.isEmpty();
    }
    
    @Override
    public int getSize() {
        return students.size();
    }
    
    @Override
    public String getStudentsList() {
        StringBuilder sb = new  StringBuilder();
        for(int indx = 0; indx < students.size(); indx++) {
            sb.append(students.get(indx).toString());
            sb.append("\n");
            sb.append(students.get(indx).getProffesionalDescription());
        }
        return sb.toString();
    }
        
    private int getStudentIndex(Name name) {
        for(int index = 0; index < students.size(); index++) {
            if(students.get(index).getName().equals(name)) {
		return index;
            }
	}
        return -1;
    }
}